package com.example.project.service;

import com.example.project.dao.RolesInterface;
import com.example.project.entity.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RolesService {
    @Resource
    RolesInterface rolesInterface;
    public ResponseInfo getroletable(Roles roles, RequestInto params) {
        params.setStart((params.getPage()-1)*params.getSize());
        List<Menu> list = rolesInterface.queryTable(roles,params);
        long total=rolesInterface.queryTableCount(roles,params);
        Map<String, Object> map = new HashMap<>();
        map.put("rows", list);
        map.put("total", total);

        return  ResponseInfo.success(map);
    }

    public ResponseInfo addrole(Roles roles) {
        rolesInterface.addrole(roles);
        return  ResponseInfo.success(null);
    }

    public ResponseInfo editrole(Roles roles) {
        rolesInterface.editrole(roles);
        return  ResponseInfo.success(null);
    }

    public ResponseInfo delrole(Roles roles) {
        rolesInterface.delrole(roles);
        return  ResponseInfo.success(null);
    }

    public ResponseInfo getuserroletable(User user) {
        List<Menu> roleList = rolesInterface.getuserroletable(user);
        Map<String, Object> map = new HashMap<>();
        map.put("rows", roleList);
        return ResponseInfo.success(map);
    }
    public ResponseInfo getusermenutable(String id) {
        List<Menu> rolemenusList = rolesInterface.getusermenutable(id);
        Map<String, Object> map = new HashMap<>();
        map.put("rows", rolemenusList);
        return ResponseInfo.success(map);
    }
// 勾选角色菜单（树状图的）

    public ResponseInfo addrolemenu(Map<String,String> map) {
        String body = map.get("body");
        body=body.substring(1,body.length()-1);
        String[] strs = body.split(",");
        rolesInterface.addrolemenu(map.get("id"),strs);
        return  ResponseInfo.success(null);
    }
    //    勾选角色菜单（下拉框的）
    public ResponseInfo adduserrole(Map<String,String> map) {
        String body = map.get("body");
        body=body.substring(1,body.length()-1);
        String[] strs = body.split(",");
        rolesInterface.adduserrole(map.get("id"),strs);
        return  ResponseInfo.success(null);
    }
}
